package org.yoao.moneymachine.business.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.yoao.crm.struts.form.MmuserForm;
import org.yoao.crm.tools.CodeFactory;
import org.yoao.crm.tools.TimeTool;
import org.yoao.moneymachine.bean.User;
import org.yoao.moneymachine.business.service.IUserService;
import org.yoao.moneymachine.dao.UserDAO;

/**
 * @author  Weishion.Yi
 * @description
 * @date 2010-10-4 下午06:10:22
 */
public class UserServiceImpl implements IUserService {
	private UserDAO userDAO = new UserDAO();

	@Override
	public int getDataCount(Map map) throws Exception {
		return userDAO.getDataCount(map);
	}

	@Override
	public List view(int startRow, int endRow, Map map) throws Exception {
		return userDAO.find(startRow, endRow, map);
	}

	@Override
	public String regedit(MmuserForm form) throws Exception {
		String result = null;
		User user = new User();
		form.setCreateTime(TimeTool.getSystemTime());
		form.setExpandCode(CodeFactory.getExpandCode());
		BeanUtils.copyProperties(user, form);
		User tmp = userDAO.findByEmail(user.getEmail());
		if(tmp == null){
			if(userDAO.insert(user) > -1)
				result = "注册成功!";
			else result = "注册失败!";
		}else{
			result = "注册邮箱已存在!";
		}
		return result;
	}

	@Override
	public void delete(String id) throws Exception {
		if(id.endsWith(",")) id = id.substring(0, id.length()-1);
		userDAO.delete(id);
	}

	@Override
	public void pay(String id) throws Exception {
		if(id.endsWith(",")) id = id.substring(0, id.length()-1);
		if(userDAO.modifyPayAmount(id) > -1)
			userDAO.modifyAmount(id, "1");
	}

	@Override
	public void upgrade(String id) throws Exception {
		if(id.endsWith(",")) id = id.substring(0, id.length()-1);
		userDAO.modifyType(id, "1");
	}

	@Override
	public MmuserForm getUser(String id) throws Exception {
		MmuserForm form = new MmuserForm();
		User user = userDAO.findById(id);
		BeanUtils.copyProperties(form, user);
		return form;
	}

	@Override
	public MmuserForm modify(MmuserForm form) throws Exception {
		User user = new User();
		BeanUtils.copyProperties(user, form);
		if(userDAO.modify(user) > -1){
			form.setInfo("数据保存成功.");
		}else  form.setInfo("数据保存失败.");
		return form;
	}
}